%Project 3 
%I. Study a plug-in-Bayes decision rule WITHOUT REJECTION using
%Parzen-window class-conditional density estimation
clearvars;clc;
%Generate classes & data

gen_classes;gen_data;
0.07;%0.78;%
Thresh = log(w2.P/w1.P);
H1 = 0.25:0.005:0.28;
H2 = 0.25:0.005:0.28;
Emin = [1,0,0];
for h1=H1
    for h2 = H2
        del11 = parzen(ds1_norm,test1,h1);
        del12 = parzen(ds2_norm,test1,h2);
        LLR1 = log(del11./del12);
        ne1 = sum(LLR1<Thresh);
        
        del21 = parzen(ds1_norm,test2,h1);
        del22 = parzen(ds2_norm,test2,h2);
        LLR2 = log(del21./del22);
        ne2 = sum(LLR2>Thresh);
        
        E = ne1 * w1.P / size(test1,1) + ne2 * w2.P / size(test2,1);
        if E < Emin(1)
            Emin(1) = E; Emin(2) = h1; Emin(3) =  h2;
            disp(Emin)
        end
    end
end

h1 = Emin(2); h2 = Emin(3);
disp(['best H_1 ',num2str(h1),'     best H_2 ',num2str(h2)])
del11 = parzen(ds1_norm,test1,h1);
del12 = parzen(ds2_norm,test1,h2);
LLR1 = log(del11./del12);
ne1 = sum(LLR1<Thresh);

del21 = parzen(ds1_norm,test2,h1);
del22 = parzen(ds2_norm,test2,h2);
LLR2 = log(del21./del22);
ne2 = sum(LLR2>Thresh);

E = ne1 * w1.P / size(test1,1) + ne2 * w2.P / size(test2,1);
disp('=======Parzen error for test =======')
disp(['E_Parzen = ',num2str(E)])

figure(1)
[counts_A, bins_A] = hist(LLR1,200);
hist(LLR1,200);
hold on
vec = min(LLR2):(bins_A(2) - bins_A(1)):max(LLR2);
[counts_B, bins_B]  = hist(LLR2,vec);
hist(LLR2,vec);
h = findobj(gca,'Type','patch');
set(h(2),'FaceColor','m','FaceAlpha',0.4);
set(h(1),'FaceColor','c','FaceAlpha',0.4);
xlim([-4 6])
xlabel('LLR')
title(['Histogram for h_1=',num2str(h1),' and h_2=',num2str(h2)])
line([Thresh Thresh],[0 max(max(counts_A),max(counts_B))],'color','r','Linewidth',2)
hold off

disp('=======Parzen error for new =======')
del11 = parzen(ds1_norm,new1,h1);
del12 = parzen(ds2_norm,new1,h2);
LLR1 = log(del11./del12);
ne1 = sum(LLR1<Thresh);

del21 = parzen(ds1_norm,new2,h1);
del22 = parzen(ds2_norm,new2,h2);
LLR2 = log(del21./del22);
ne2 = sum(LLR2>Thresh);

E = ne1 * w1.P / size(new1,1) + ne2 * w2.P / size(new2,1);
disp(['E_Parzen = ',num2str(E)])


figure(2)
[counts_A, bins_A] = hist(LLR1,200);
hist(LLR1,200);
hold on
vec = min(LLR2):(bins_A(2) - bins_A(1)):max(LLR2);
[counts_B, bins_B]  = hist(LLR2,vec);
hist(LLR2,vec);
h = findobj(gca,'Type','patch');
set(h(2),'FaceColor','m','FaceAlpha',0.4);
set(h(1),'FaceColor','c','FaceAlpha',0.4);
xlim([-6 6])
xlabel('LLR')
title(['Histogram for h_1=',num2str(h1),' and h_2=',num2str(h2)])
line([Thresh Thresh],[0 max(max(counts_A),max(counts_B))],'color','r','Linewidth',2)
hold off